//
//  Circle.m
//  YKL
//
//  Created by Apple on 15/12/7.
//  Copyright © 2015年 Apple. All rights reserved.
//

#import "YZNumCircle.h"

#define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]

@interface YZNumCircle ()
{
    CAShapeLayer* _progressLayerNormal;
    CAShapeLayer* _progressLayerWarning;
    
    CAGradientLayer *_gradientLayerWarning;
}

@end

@implementation YZNumCircle


-(instancetype)initWithFrame:(CGRect)frame lineWidth:(float)lineWidth
{
    self = [super initWithFrame:frame];
    if (self) {
        _lineWidth = lineWidth;
        [self buildLayout];
    }
    return self;
}

-(void)buildLayout
{
    float centerX = self.bounds.size.width/2.0;
    float centerY = self.bounds.size.height/2.0;
    //半径
    float radius = (self.bounds.size.width-_lineWidth)/2.0;
    
    //创建贝塞尔路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(centerX, centerY)
                                                        radius:radius
                                                    startAngle:(-1.34*M_PI)
                                                      endAngle:.34f*M_PI
                                                     clockwise:YES];
    
    
    //创建进度layer
    _progressLayerWarning = [CAShapeLayer layer];
    _progressLayerWarning.frame = self.bounds;
    _progressLayerWarning.fillColor =  [[UIColor clearColor] CGColor];
    //指定path的渲染颜色
    _progressLayerWarning.strokeColor  = [[UIColor blackColor] CGColor];
    _progressLayerWarning.lineCap = kCALineCapRound;
    _progressLayerWarning.lineWidth = _lineWidth;
    _progressLayerWarning.path = [path CGPath];
    _progressLayerWarning.strokeEnd = 0;
    
    //设置渐变颜色
    _gradientLayerWarning =  [CAGradientLayer layer];
    _gradientLayerWarning.frame = self.bounds;
    [_gradientLayerWarning  setColors:[NSArray arrayWithObjects:(id)[RGB(231, 231, 231) CGColor],(id)[RGB(231, 231, 231) CGColor], nil]];
    _gradientLayerWarning.startPoint = CGPointMake(0, 0);
    _gradientLayerWarning.endPoint = CGPointMake(0, 1);
    [_gradientLayerWarning setMask:_progressLayerWarning]; //用progressLayer来截取渐变层
    [self.layer addSublayer:_gradientLayerWarning];
    
    _progressLayerWarning.strokeEnd = 1;
    [_progressLayerWarning removeAllAnimations];
    
    
    
    
    
//    //创建进度layer
//    _progressLayerNormal = [CAShapeLayer layer];
//    _progressLayerNormal.frame = self.bounds;
//    _progressLayerNormal.fillColor =  [[UIColor clearColor] CGColor];
//    //指定path的渲染颜色
//    _progressLayerNormal.strokeColor  = [[UIColor blackColor] CGColor];
//    _progressLayerNormal.lineCap = kCALineCapRound;
//    _progressLayerNormal.lineWidth = _lineWidth;
//    _progressLayerNormal.path = [path CGPath];
//    _progressLayerNormal.strokeEnd = 0;
//
//    //设置渐变颜色
//    CAGradientLayer *gradientLayerNormal =  [CAGradientLayer layer];
//    gradientLayerNormal.frame = self.bounds;
//    [gradientLayerNormal setColors:[NSArray arrayWithObjects:(id)[RGB(231, 231, 231) CGColor],(id)[RGB(231, 231, 231) CGColor], nil]];
//    gradientLayerNormal.startPoint = CGPointMake(0, 0);
//    gradientLayerNormal.endPoint = CGPointMake(0, .5);
//    [gradientLayerNormal setMask:_progressLayerNormal]; //用progressLayer来截取渐变层
//    [self.layer addSublayer:gradientLayerNormal];
//
//    _progressLayerNormal.strokeEnd = 1;
//    [_progressLayerNormal removeAllAnimations];
}

- (void)changeColorType:(NSInteger)colorType {
    
    switch (colorType) {
        case 1:
        {
            // 黄色
            [_gradientLayerWarning  setColors:[NSArray arrayWithObjects:(id)[kColorWithHex(0xE89219) CGColor],(id)[kColorWithHex(0xFFDFB1) CGColor], nil]];
        }
            break;
            
        case 2:
        {
            // 红色
            [_gradientLayerWarning  setColors:[NSArray arrayWithObjects:(id)[RGB(238, 30, 61) CGColor],(id)[RGB(255, 103, 117) CGColor], nil]];
        }
            break;
            
        default:
        {
            // 默认色
            [_gradientLayerWarning  setColors:[NSArray arrayWithObjects:(id)[RGB(231, 231, 231) CGColor],(id)[RGB(231, 231, 231) CGColor], nil]];
        }
            break;
    }
}

@end
